
import { getuserinfo, login, userPic } from '@/api/user'
import { getToken, setToken, removeToken, setTime, getTime, removeTime } from '@/utils/auth'
import { resetRouter } from '@/router'
const state = {
  token: getToken(),
  userinfo: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  setuserinfo(state, data) {
    state.userinfo = { ...data }
  },
  removeuserinfo(state) {
    state.userinfo = {}
  },
  removeTime() {
    removeTime()
  }
}
const actions = {
  async login(store, data) {
    // login(data).then(res => {
    //   console.log(res)
    //   store.commit('setToken', res.data.data)
    // })
    const res = await login(data)
    console.log(res)
    store.commit('setToken', res)
    setTime()
    const aa = getTime()
    console.log(aa)
  },
  async getuserinfo(store) {
    const res = await getuserinfo()
    const Pic = await userPic(res.userId)
    store.commit('setuserinfo', { ...res, ...Pic })
  },
  logout(store) {
    resetRouter()
    store.commit('permission/setrouter', [], { root: true })
    store.commit('removeToken')
    store.commit('removeuserinfo')
    store.commit('removeTime')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
